package com.ymatou.iapi.trading.useranalysis.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymatou.iapi.trading.useranalysis.parameter.BatchBuyerFlagQueryReq;
import com.ymatou.iapi.trading.useranalysis.service.BatchBuyerFlagQueryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;

public class Ts_BatchBuyerFlagQuery {
	
	private static BatchBuyerFlagQueryReq req;
	private static BatchBuyerFlagQueryCall call;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询用户标签信息集合");
	}

	@Before
	public void caseUp() {
		req = new BatchBuyerFlagQueryReq();
		call = new BatchBuyerFlagQueryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchBuyerFlagQuery_001() {
		Logger.start(true, "查询标签为vip的用户");
		try {

			//disconf固定配置为92
			List<Long> buyerIds = new ArrayList<Long>();
			buyerIds.add(92L);
			
			req.setBuyerIds(buyerIds);

			call.setData(req);
			call.callService();
			
			Logger.verifyEquals(true, call.getSuccess(), "验证success");
			Logger.verifyEquals(2, call.getFlags().optJSONArray(buyerIds.get(0).toString()).get(0), "验证flag");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchBuyerFlagQuery_002() {
		Logger.start(true, "查询标签为新客的用户");
		try {
			
			Logger.comment("数据准备开始");
			OrderCallService orderCallService = new OrderCallService();

			int userId = orderCallService.createNewUser();
			TradingServiceResp resp = TradingPlaceOrderCallService.placeOrderWithUserId(userId);
			Logger.comment("数据准备结束");
			
			TradingSyncService.getSyncResult(resp.getOrderList().get(0).getOrderId());
			
			List<Long> buyerIds = new ArrayList<Long>();
			buyerIds.add((long)userId);
			
			req.setBuyerIds(buyerIds);

			call.setData(req);
			call.callService();
			
			Logger.verifyEquals(true, call.getSuccess(), "验证success");
			Logger.verifyEquals(1, call.getFlags().optJSONArray(buyerIds.get(0).toString()).get(0), "验证flag");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchBuyerFlagQuery_003() {
		Logger.start(true, "批量查询标签为新客和vip的用户");
		try {
			
			Logger.comment("数据准备开始");
			OrderCallService orderCallService = new OrderCallService();

			int userId = orderCallService.createNewUser();
			TradingServiceResp resp = TradingPlaceOrderCallService.placeOrderWithUserId(userId);
			Logger.comment("数据准备结束");
			
			TradingSyncService.getSyncResult(resp.getOrderList().get(0).getOrderId());
			
			List<Long> buyerIds = new ArrayList<Long>();
			buyerIds.add((long)userId);
			buyerIds.add(92L);
			
			req.setBuyerIds(buyerIds);

			call.setData(req);
			call.callService();
			
			Logger.verifyEquals(true, call.getSuccess(), "验证success");
			Logger.verifyEquals(1, call.getFlags().optJSONArray(buyerIds.get(0).toString()).get(0), "验证flag");
			Logger.verifyEquals(2, call.getFlags().optJSONArray(buyerIds.get(1).toString()).get(0), "验证flag");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchBuyerFlagQuery_004() {
		Logger.start(true, "查询标签非vip、非新客的用户");
		try {

			List<Long> buyerIds = new ArrayList<Long>();
			buyerIds.add(93L);
			
			req.setBuyerIds(buyerIds);

			call.setData(req);
			call.callService();
			
			Logger.verifyEquals(true, call.getSuccess(), "验证success");
			Logger.verifyEquals(0, call.getFlags().optJSONArray(buyerIds.get(0).toString()).length(), "验证flag");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
